<?php
/**
 * 生成excel文件操作
 *
 * @author wesley wu
 * @date 2013.12.9
 */
namespace Admin\Common;
class Excel
{

    private $limit = 100000;

    public function download($data, $fileName,$ie)
    {

        $fileName = $this->_charset($fileName,$ie);
        header("Content-Type: application/vnd.ms-excel; charset=".(empty($ie)?'GBK':'UTF-8'));
        header("Content-Disposition:inline;filename=\"" .$fileName  . ".xls\"");

        echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n
        <Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"
        xmlns:x=\"urn:schemas-microsoft-com:office:excel\"
        xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"
        xmlns:html=\"http://www.w3.org/TR/REC-html40\">";
        echo "\n<Worksheet ss:Name=\"" . $fileName . "\">\n<Table>\n";
        $guard = 0;
        foreach($data as $v)
        {
            $guard++;
            if($guard==$this->limit)
            {
                ob_flush();
                flush();
                $guard = 0;
            }
            echo $this->_addRow($this->_charset($v,$ie));
        }
        echo "</Table>\n</Worksheet>\n</Workbook>";
    }

    private function _addRow($row)
    {
        $cells = "";
        foreach ($row as $k => $v)
        {
            $cells .= "<Cell><Data ss:Type=\"String\">" . $v . "</Data></Cell>\n";
        }
        return "<Row>\n" . $cells . "</Row>\n";
    }

    private function _charset($data,$ie)
    {
        if(!$data)
        {
            return false;
        }
        if(is_array($data))
        {
            foreach($data as $k=>$v)
            {
                $data[$k] = $this->_charset($v,$ie);
            }
            return $data;
        }
        if($ie){
            return iconv('UTF-8', 'GBK', $data);
        }else{
            return $data;
        }
    }

    /*
    $csv = new Csv();    //实例化类
    $xlsName = iconv('UTF-8','gb2312',$title);
    $csv_title = array('单号','生成日期','商品个数','订单金额($)','含GST($)','提货状态','实际备货数量','是否全部备货');
    $data = $this->M_bizClientHand->GetCollectionDataExcel($this->m_Uid);
    $csv->put_csv($data,$csv_title,$xlsName);  //调用类库导出
    */
    public function put_csv($list,$title,$csvTitle){
      	$file_name=$csvTitle.".csv";
   	    header('Content-Type: text/csv; CHARSET=gb2312');
      	header ( 'Content-Type: application/vnd.ms-excel' );
      	header ( 'Content-Disposition: attachment;filename='.$file_name );
      	header ( 'Cache-Control: max-age=0' );
      	$file =fopen('php://output',"a");
      	$limit=1000;
      	$calc=0;
      	foreach ($title as $v){
      		$tit[]=iconv('UTF-8', 'GB2312//IGNORE',$v);
      	}
      	fputcsv($file,$tit);
      	foreach ($list as $v){
      		$calc++;
      		if($limit==$calc){
      			ob_flush();
      			flush();
      			$calc=0;
      		}
      		foreach ($v as $t){
   			      $t=is_numeric($t)?$t."\t":$t;
      			  $tarr[]=iconv('UTF-8', 'GB2312//IGNORE',$t);
      		}
   		//var_dump($tarr);
      		fputcsv($file,$tarr);
      		unset($tarr);
      	}
      	unset($list);
      	fclose($file);
      	exit();
    }


    public function download_together($data, $fileName)
    {

         if(!empty($data) ){
         $fileName = iconv("UTF-8", "GBK", $fileName);
         if (!is_dir(ROOT_PATH.'Public/Excel/order_idcard_img')) mkdir(ROOT_PATH.'Public/Excel/order_idcard_img');
        @$fp = fopen(ROOT_PATH.'Public/Excel/order_idcard_img/'.$fileName,'w');
         foreach ($data as $key => $value) {
                @fputcsv($fp,$value);
           }
        @fclose($file);
         }
    }

}



////使用方法
//$excel = new Excel();
//$data = array(
//    array('姓名','标题','文章','价格','数据5','数据6','数据7'),
//    array('数据1','数据2','数据3','数据4','数据5','数据6','数据7'),
//    array('数据1','数据2','数据3','数据4','数据5','数据6','数据7'),
//    array('数据1','数据2','数据3','数据4','数据5','数据6','数据7'),
//    array('数据1','数据2','数据3','数据4','数据5','数据6','数据7'),
//    array('数据1','数据2','数据3','数据4','数据5','数据6','数据7')
//);
//$excel->download($data, '这是一个测试');

?>
